#include "peakwidget.h"
#include "dm/dmdef.h"

PeakWidget::PeakWidget(const uint8_t &channel, const double &gainMax, const QStringList &items, const QString &title, QWidget *parent)
    : AGroupBox{title, parent}
    , m_channel{channel}
{
    setFixedHeight(420);
    comboBox = new AComboBox(this);
    bar = new PeakBar(this);
    slider = new ASlider(this);
    spinBox = new ADoubleSpinBox(this);
    speakerButton = new APushButton(this);

    comboBox->setFixedWidth(DevEnvParam::CTRL_FIX_WIDTH);
    comboBox->addItems(items);
    comboBox->setCurrentIndex(channel & 0x0F);

    slider->setMaximumHeight(500);
    slider->setRange(GAIN_MIN / GAIN_STEP, gainMax / GAIN_STEP);

    spinBox->setRange(GAIN_MIN, gainMax);
    spinBox->setDecimals(GAIN_DECIMAL);
    spinBox->setSingleStep(GAIN_STEP);
    spinBox->setSuffix(tr(LABEL_DECIBEL_UINT));

    speakerButton->setCheckable(true);

    speakerButton->setStyleSheet(QString(R"(
        APushButton {
            border-radius: %1px;
            border: none;
            background-color: #2F8C00;
            icon: url(:/img/volume-on.svg);
            icon-size: %2px %2px;
        }
        /* 悬停 */
        APushButton:hover {
            background-color: #CCCCCC;
        }
        /* 鼠标按下 */
        APushButton:pressed {
            background-color: #CCCCCC;
        }
        /* 选中状态 */
        APushButton:checked {
            background-color: #CC3131;
            icon: url(:/img/volume-off.svg);
            icon-size: %2px %2px;
        }
        /* 选中状态悬停 */
        APushButton:checked:hover {
            background-color: #CCCCCC;
        }
        /* 选中状态鼠标按下 */
        APushButton:checked:pressed {
            background-color: #CCCCCC;
        }
        /* 禁用状态 */
        APushButton:disabled {
            background-color: #999999;
        }
    )").arg(4 * ratio()).arg(15 * ratio()));

    auto layout = new QGridLayout(this);
    layout->addWidget(bar, 0, 0, Qt::AlignRight);
    layout->addWidget(slider, 0, 1, Qt::AlignLeft);
    layout->addWidget(comboBox, 1, 0, 1, 2, Qt::AlignHCenter);
    layout->addWidget(spinBox, 2, 0, 1, 2, Qt::AlignHCenter);
    layout->addWidget(speakerButton, 3, 0, 1, 2, Qt::AlignHCenter);
    layout->setVerticalSpacing(15 * ratio());

    connect(comboBox, QOverload<int>::of(&AComboBox::currentIndexChanged), this, &PeakWidget::comboBoxChanged);
    connect(slider, QOverload<int>::of(&ASlider::valueChanged), this, &PeakWidget::sliderChanged);
    connect(spinBox, QOverload<double>::of(&ADoubleSpinBox::valueChanged), this, &PeakWidget::spinBoxChanged);
    connect(speakerButton, &QPushButton::clicked, this, &PeakWidget::speakerToggled);
}

uint8_t PeakWidget::channel() const
{
    return m_channel;
}

void PeakWidget::set(uint16_t gain, uint8_t speaker)
{
    blockSignals(true);

    slider->setValue(static_cast<int>(gain) + slider->minimum());

    speakerButton->setChecked(speaker);
    speakerToggled(speaker);

    blockSignals(false);
}

void PeakWidget::peak(const uint8_t &value)
{
    bar->peak(value);
}

void PeakWidget::comboBoxChanged(const int &index)
{
    if (index > -1) {
        emit group(m_channel, index);
    }
}

void PeakWidget::sliderChanged(const int &value)
{
    spinBox->setValue(value * GAIN_STEP);
}

void PeakWidget::spinBoxChanged(const double &value)
{
    slider->blockSignals(true);
    slider->setValue(value / GAIN_STEP);
    slider->blockSignals(false);
    emit gainChanged(m_channel, slider->value() - slider->minimum());
}

void PeakWidget::speakerToggled(bool checked)
{
    // if (checked) {
    //     speakerButton->setIcon(QIcon(":/img/volume-off.svg"));
    // }
    // else {
    //     speakerButton->setIcon(QIcon(":/img/volume-on.svg"));
    // }
    emit speakerChanged(m_channel, checked);
}

void PeakWidget::setBlockSignals(bool block)
{
    slider->blockSignals(block);
    spinBox->blockSignals(block);
    speakerButton->blockSignals(block);
}
